﻿using System;
using System.Data.Services.Client;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using SimplePoll.Web.Models;

namespace SimplePoll.Web.DataAccess
{
    public class PollResultRepository
    {
        readonly CloudStorageAccount account;

        public PollResultRepository()
        {
            account = CloudConfiguration.GetStorageAccount("DataConnectionString");
        }

        static int CalculatePercentage(decimal total, decimal answerTotal)
        {
            decimal percent = answerTotal/total;
            decimal smallPercent = Math.Round(percent, 2)*100;
            int fullPercent = int.Parse(Math.Round(smallPercent, 0).ToString());
            return fullPercent;
        }

        public void Create(string name)
        {
            var context = new PollDataContext(account);

            var row = new PollResultRow {RowKey = TableKey.Value, Name = name};
            context.AddObject(PollDataContext.PollResultTable, row);
            context.SaveChanges();
        }

        public PollResult Retrieve()
        {
            Poll poll = new PollRepository().Retrieve();
            var context = new PollDataContext(account) {MergeOption = MergeOption.NoTracking};
            PollResultRow resultRow = Retrieve(context, poll.Name);

            var pollResult = new PollResult
                                 {
                                     Answer1 = poll.Answer1,
                                     Answer1Percentage = CalculatePercentage(resultRow.TotalAnswers, resultRow.TotalQ1Answers),
                                     Answer2 = poll.Answer2,
                                     Answer2Percentage = CalculatePercentage(resultRow.TotalAnswers, resultRow.TotalQ2Answers),
                                     Answer3 = poll.Answer3,
                                     Answer3Percentage = CalculatePercentage(resultRow.TotalAnswers, resultRow.TotalQ3Answers),
                                     Question = poll.Question,
                                     TotalVotes = resultRow.TotalAnswers
                                 };

            return pollResult;
        }


        static PollResultRow Retrieve(PollDataContext context, string name)
        {
            var query = (from result in context.PollResults
                         where result.PartitionKey == name.Encode()
                         select result).AsTableServiceQuery();
            return query.Execute().SingleOrDefault();
        }

        public void Update(string name, int answer)
        {
            var context = new PollDataContext(account);

            PollResultRow row = Retrieve(context, name);

            row.TotalAnswers += 1;
            switch (answer)
            {
                case 1:
                    row.TotalQ1Answers += 1;
                    break;
                case 2:
                    row.TotalQ2Answers += 1;
                    break;
                case 3:
                    row.TotalQ3Answers += 1;
                    break;
            }

            context.UpdateObject(row);
            context.SaveChanges();
        }
    }
}